<template>
    <van-address-edit :area-list="areaList" show-postal show-delete show-set-default show-search-result
        :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave"
        @delete="onDelete" @change-detail="onChangeDetail" />
</template>

<script>

import { areaList } from '@vant/area-data';
import axios from 'axios';
let service = axios.create();
export default {
    data() {
        return {
            areaList,
            searchResult: [],
            id: ''

        };
    },
    methods: {
        onSave(text) {
            // console.log(text);
            if (text.isDefault) {                                  //如果增加的是默认地址
                service({
                    url: `http://localhost:3000/address/`,           //发请求获取其他为默认地址的地址
                    method: 'get',
                    params: {
                        isDefault: true
                    }
                }).then(res => {
                    console.log(res.data)
                    if (res.data != '') {                             //如果有其他默认地址，那么
                        console.log(res.data)
                        this.id = res.data
                        // console.log(this.id[0].id)
                        service({
                            url: `http://localhost:3000/address/${this.id[0].id}`,      //发送请求把别的默认改成不是默认
                            method: 'patch',
                            data: {
                                isDefault: false
                            }
                        }).then(res => {
                            console.log('chenggong')
                            service({
                                url: "http://localhost:3000/address",        //发送请求把在输入的改成默认
                                method: "post",
                                data: {
                                    name: text.name,
                                    tel: text.tel,
                                    country: text.country,
                                    province: text.province,
                                    city: text.city,
                                    county: text.county,
                                    areaCode: text.areaCode,
                                    postalCode: text.postalCode,
                                    addressDetail: text.addressDetail,
                                    isDefault: text.isDefault,
                                    userid: sessionStorage.getItem('userid'),

                                }
                            }).then(res => {
                                this.$router.push('/Myaddress')
                            });
                        })
                    }
                    else {                                                //如果没有其他默认，就直接增加进去
                        service({
                            url: "http://localhost:3000/address",
                            method: "post",
                            data: {
                                name: text.name,
                                tel: text.tel,
                                country: text.country,
                                province: text.province,
                                city: text.city,
                                county: text.county,
                                areaCode: text.areaCode,
                                postalCode: text.postalCode,
                                addressDetail: text.addressDetail,
                                isDefault: text.isDefault,
                                userid: sessionStorage.getItem('userid'),

                            }
                        }).then(res => {
                            this.$router.push('/Myaddress')
                        });

                    }

                })

            } else {
                service({
                    url: "http://localhost:3000/address",                 //如果增加的不是默认地址，那么就直接增加
                    method: "post",
                    data: {
                        name: text.name,
                        tel: text.tel,
                        country: text.country,
                        province: text.province,
                        city: text.city,
                        county: text.county,
                        areaCode: text.areaCode,
                        postalCode: text.postalCode,
                        addressDetail: text.addressDetail,
                        isDefault: text.isDefault,
                        userid: sessionStorage.getItem('userid'),

                    }
                }).then(res => {
                    this.$router.push('/Myaddress')
                });
            }


        },
        onDelete() {
            Toast('delete');
        },
        onChangeDetail(val) {
            if (val) {
                this.searchResult = [
                    {
                        name: '立人科技园',
                        address: '西安市雁塔区',
                    },
                ];
            } else {
                this.searchResult = [];
            }
        },
    },
}
</script>

<style>

</style>